<template>
  <el-dialog
    title="编辑会员"
    v-model="dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      :model="memberForm"
      label-width="100px"
      ref=" member-Form"
      :rules="rules"
      class="demo-memberForm"
    >
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="memberForm.nickname"
          placeholder="请输入昵称"
          auto-complete="off"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="memberForm.phone"
          placeholder="请输入手机号"
          auto-complete="off"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          :disabled="editId ? true : false"
          v-model="memberForm.password"
          placeholder="请输入密码"
          auto-complete="off"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="memberForm.status" :label="1">启用</el-radio>
        <el-radio v-model="memberForm.status" :label="2">禁用</el-radio>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" @click="submitMember()"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";

// 导入store
import useMemberStore from "../../stores/member";
// 定义
const member = useMemberStore();

// 定义控制弹窗显隐的变量
const dialogVisible = ref(false);

//关闭弹窗？
const handleClose = (done) => {
  ElMessageBox.confirm("确定关闭吗?", "关闭弹窗！", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      done();
      //   重置表单
      resetForm();
    })
    .catch(() => {
      // catch error
    });
};

// 关闭弹窗
const closeDialog = () => {
  resetForm();
};

// 定义菜单表单
let memberForm = reactive({
  nickname: "",
  phone: "",
  password: "",
  status: 1,
});

// 定义编辑id
let editId = ref("");

// 表单规则
const rules = reactive({
  nickname: { required: true, message: "请输入昵称", trigger: "blur" },
  phone: { required: true, message: "请输入手机号", trigger: "blur" },
  password: { required: true, message: "请输入密码", trigger: "blur" },
  status: { required: true, message: "请选择状态", trigger: "blur" },
});

// 清空表单，关闭弹窗
const resetForm = () => {
  // 重置编辑id
  editId = ref("");
  // 初始化表单
  memberForm = reactive({
    nickname: "",
    phone: "",
    password: "",
    status: 1,
  });
  // 关闭弹窗
  dialogVisible.value = false;
};

// 提交表单
const submitMember = () => {
//   console.log(" memberForm", memberForm);
  member.editMemberAction({ ...memberForm, id: editId.value });
  resetForm();
};

// 接收父组件数据
const acceptValue = (value) => {
  //   console.log("value", value);
  editId.value = value.id;
  memberForm = reactive({ ...value });
};

// 将变量与函数暴露出来,此代码片段放到下面
defineExpose({
  dialogVisible,
  acceptValue,
});
</script>

<style lang="css" scoped></style>
